﻿using Microsoft.CSharp.Activities;
using System;
using System.Activities;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WFExtended.Twitter.Activities.Designers;

namespace WFExtended.Twitter.Activities
{
    /// <summary>
    /// Gets a collection of relevant Tweets matching a specified query
    /// </summary>
    [ToolboxBitmapAttribute(typeof(EntryPoint), "Resources.SearchTweetsToolbox.bmp")]
    [Designer(typeof(SearchTweetsDesigner))]
    [Description("Gets a collection of relevant Tweets matching a specified query")]
    public class SearchTweets : TwitterOperationInvoker<SearchTweetsResult>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SearchTweets"/> class.
        /// </summary>
        public SearchTweets()
            : base("SearchTweetsWithOptions")
        {
            Query = new InArgument<string>();
            ResultTypeArgument = new InArgument<string>();
            Count = new InArgument<int>(new CSharpValue<int>("15"));
            GeoCode = new InArgument<string>();
            Until = new InArgument<DateTime>();
            SinceID = new InArgument<string>();
            MaxID = new InArgument<string>();
            IncludeEntities = new InArgument<bool>(new CSharpValue<bool>("true"));
        }

        /// <summary>
        /// Gets or sets a UTF-8, URL-encoded search query of 1,000 characters maximum, including operators
        /// <remarks>
        /// Required Argument
        /// </remarks>
        /// </summary>
        /// <value>
        /// Search query.
        /// </value>
        [Browsable(true)]
        [RequiredArgument]
        [Category("Parameters")]
        [Description("Search query")]
        public InArgument<string> Query { get; set; }

        /// <summary>
        /// Gets or sets the lang to restricts tweets to the given language
        /// <remarks>
        /// ISO 639-1 code, Example Values: fr
        /// </remarks>
        /// </summary>
        /// <value>
        /// The code lang
        /// </value>
        [Browsable(true)]
        [Category("Parameters")]
        [Description("The code lang")]
        public InArgument<string> Lang { get; set; }


        /// <summary>
        /// Specifies what type of search results you would prefer to receive
        /// </summary>
        /// <value>
        /// Type of search results
        /// </value>
        [Browsable(true)]
        [Category("Parameters")]
        [Description("Type of search results")]
        public SearchResultType ResultType { get; set; }

        [Browsable(false)]
        public InArgument<string> ResultTypeArgument { get; set; }

        /// <summary>
        /// Returns tweets by users located within a given radius of the given latitude/longitude.
        /// <remarks>
        /// Format "latitude,longitude,radius". 
        /// Example Values: 37.781157,-122.398720,1mi, radius units must be specified as either "mi" (miles) or "km" (kilometers)
        /// </remarks>
        /// </summary>
        /// <value>
        /// "latitude,longitude,radius"
        /// </value>
        [Browsable(true)]
        [Category("Parameters")]
        [Description("latitude,longitude,radius")]
        public InArgument<string> GeoCode { get; set; }

        /// <summary>
        /// Returns tweets generated before the given date.
        /// </summary>
        /// <value>
        /// Returns tweets generated before the given date
        /// </value>
        [Browsable(true)]
        [Category("Navigation")]
        [Description("Returns tweets generated before the given date.")]
        public InArgument<DateTime> Until { get; set; }

        [Browsable(false)]
        public InArgument<string> UntilStringArgument { get; set; }

        /// <summary>
        /// Returns results with an ID greater than (that is, more recent than) the specified ID. 
        /// </summary>
        /// <value>
        /// Results with an ID greater than
        /// </value>
        [Browsable(true)]
        [Category("Navigation")]
        [Description("Results with an ID greater than")]
        public InArgument<string> SinceID { get; set; }

        /// <summary>
        /// Returns results with an ID less than (that is, older than) or equal to the specified ID.
        /// </summary>
        /// <value>
        /// Results with an ID less than
        /// </value>
        [Browsable(true)]
        [Category("Navigation")]
        [Description("Results with an ID less than")]
        public InArgument<string> MaxID { get; set; }       


        /// <summary>
        /// The number of tweets to return per page, up to a maximum of 100. Defaults to 15
        /// <remarks>Example Values: 100</remarks>
        /// </summary>
        /// <value>
        /// Number of tweets per page
        /// </value>
        [Browsable(true)]
        [Category("Navigation")]
        [Description("Number of tweets per page")]
        [RequiredArgument]
        public InArgument<int> Count { get; set; }

        /// <summary>
        /// The entities node will be disincluded when set to false.
        /// </summary>
        /// <value>
        /// The entities node will be disincluded when set to false
        /// </value>
        [Browsable(true)]
        [Category("Parameters")]
        [Description("The entities node will be disincluded when set to false")]
        [RequiredArgument]
        public InArgument<bool> IncludeEntities { get; set; }

        /// <summary>
        /// Caches the metadata.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            RuntimeArgument queryArgument = new RuntimeArgument("Query", typeof(string), ArgumentDirection.In, true);
            metadata.Bind(Query, queryArgument);
            metadata.AddArgument(queryArgument);

            RuntimeArgument resultTypeArgument = new RuntimeArgument("ResultTypeArgument", typeof(string), ArgumentDirection.In, true);
            metadata.Bind(ResultTypeArgument, resultTypeArgument);
            metadata.AddArgument(resultTypeArgument);

            RuntimeArgument countArgument = new RuntimeArgument("Count", typeof(int), ArgumentDirection.In, true);
            metadata.Bind(Count, countArgument);
            metadata.AddArgument(countArgument);

            RuntimeArgument geoCodeArgument = new RuntimeArgument("GeoCode", typeof(string), ArgumentDirection.In, false);
            metadata.Bind(GeoCode, geoCodeArgument);
            metadata.AddArgument(geoCodeArgument);

            RuntimeArgument untilArgument = new RuntimeArgument("Until", typeof(DateTime), ArgumentDirection.In, false);
            metadata.Bind(Until, untilArgument);
            metadata.AddArgument(untilArgument);

            RuntimeArgument untilStringArgument = new RuntimeArgument("UntilStringArgument", typeof(string), ArgumentDirection.In, false);
            metadata.Bind(UntilStringArgument, untilStringArgument);
            metadata.AddArgument(untilStringArgument);

            RuntimeArgument sinceIDArgument = new RuntimeArgument("SinceID", typeof(string), ArgumentDirection.In, false);
            metadata.Bind(SinceID, sinceIDArgument);
            metadata.AddArgument(sinceIDArgument);

            RuntimeArgument maxIDArgument = new RuntimeArgument("MaxID", typeof(string), ArgumentDirection.In, false);
            metadata.Bind(MaxID, maxIDArgument);
            metadata.AddArgument(maxIDArgument);

            RuntimeArgument langArgument = new RuntimeArgument("Lang", typeof(string), ArgumentDirection.In, false);
            metadata.Bind(Lang, langArgument);
            metadata.AddArgument(langArgument);

            RuntimeArgument includeEntitiesArgument = new RuntimeArgument("IncludeEntities", typeof(bool), ArgumentDirection.In, true);
            metadata.Bind(IncludeEntities, includeEntitiesArgument);
            metadata.AddArgument(includeEntitiesArgument);


            base.CacheMetadata(metadata);
        }

        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        protected override void Execute(NativeActivityContext context)
        {
            Parameters.Clear();

            Parameters.Add("searchQuery", Query);
            Parameters.Add("lang", Lang);
            ResultTypeArgument.Set(context, ResultType.ToString());
            Parameters.Add("resultType", ResultTypeArgument);
            Parameters.Add("geocode", GeoCode);
            if(Until.Get(context) != DateTime.MinValue)
                UntilStringArgument.Set(context, String.Format("{0:yyyy-MM-dd}", Until.Get(context)));
            Parameters.Add("until", UntilStringArgument);
            Parameters.Add("sinceID", SinceID);
            Parameters.Add("maxID", MaxID);
            Parameters.Add("count", Count);
            Parameters.Add("includeEntities", IncludeEntities);
            
            base.Execute(context);
        }
    }
}
